import React, { PureComponent } from 'react'
import {Table, Avatar, Modal, Tooltip, Typography} from 'antd'
import {t, Trans} from "@lingui/macro"
import { Ellipsis } from 'components'
import {DropOption} from "../../../components";
import PropTypes from "prop-types";
import {getNavigationTreeTitle} from "../../../utils/tree";
const { confirm } = Modal
const { Text,Paragraph } = Typography;
import PrevieFile  from '../../../components/UploadFile/PreviewFile'
import {CopyOutlined} from '@ant-design/icons'

class List extends PureComponent {
  handleMenuClick = (record, e) => {
    const { onDeleteItem, onEditItem  }= this.props

    if (e.key === '1') {
      onEditItem(record)
    } else if (e.key === '2') {
      confirm({
        title: t`Are you sure delete this record?`,
        onOk() {
          onDeleteItem(record.id)
        },
      })
    }
  }

  render() {
    const { options,trees,...tableProps } = this.props
    const columns = [
      {
        title: t`Id`,
        dataIndex: 'id',
        key: 'id',
      },
      {
        title: t`Url`,
        dataIndex: 'url',
        key: 'url',
           render:(text, recode) => {
             return <Paragraph copyable={{text: text}}> </Paragraph>
        }
      },
      {
        title: t`File`,
        dataIndex: 'url',
        key: 'file',
           render:(text, recode) => {

              return <PrevieFile extension={recode.extension} width={60} height={60} fileType={recode.fileType} url={recode.url} onClick={()=>{
                window.open(recode.url)
              }} />
        }
      },
      {
        title: t`File Type`,
        dataIndex: 'fileType',
        key: 'fileType',
        render:(text, recode) => {
          if(text != null && text.length>60) {
            const start = text.slice(0, 120).trim();
            return <Text style={{ maxWidth: '100%' }} ellipsis={ "..." }>{start}</Text>
          } else {
            return text
          }
        }
      },
      {
        title: t`Extension`,
        dataIndex: 'extension',
        key: 'extension',
        render:(text, recode) => {
          if(text != null && text.length>60) {
            const start = text.slice(0, 120).trim();
            return <Text style={{ maxWidth: '100%' }} ellipsis={ "..." }>{start}</Text>
          } else {
            return text
          }
        }
      },
      {
        title: t`File Size`,
        dataIndex: 'size',
        key: 'size',
      },
      {
        title: t`File Hash`,
        dataIndex: 'hash',
        key: 'hash',
      },
      {
        title: t`Ref Count`,
        dataIndex: 'refCount',
        key: 'refCount',
      },
      {
        title: t`Last Ref Time`,
        dataIndex: 'lastRefTime',
        key: 'lastRefTime',
      },
      {
        title: t`Updated Time`,
        dataIndex: 'updatedTime',
        key: 'updatedTime',
      },
      {
        title: t`Created Time`,
        dataIndex: 'createdTime',
        key: 'createdTime',
      },
      {
        title: <Trans>Operation</Trans>,
        key: 'operation',
        fixed: 'right',
        width: '8%',
        render: (text, record) => {
          return (
            <DropOption
              onMenuClick={e => this.handleMenuClick(record, e)}
              menuOptions={[
                { key: '1', name: t`Update` },
                { key: '2', name: t`Delete` },
              ]}
            />
          )
        },
      },
    ]

    return (
      <Table
        {...tableProps}
        pagination={{
          ...tableProps.pagination,
          showTotal: (total)=>{return t`Total ` + total + t` Items`},
        }}
        bordered
        scroll={{ x: 1200 }}
        columns={columns}
        simple
        rowKey={record => record.id}
      />
    )
  }
}

List.propTypes = {
  onDeleteItem: PropTypes.func,
  onEditItem: PropTypes.func,
  location: PropTypes.object,
}

export default List
